package com.job.servicejob.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.job.commonutils.R;
import com.job.servicejob.entity.Job;
import com.job.servicejob.entity.vo.JobQuery;
import com.job.servicejob.service.JobService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author myt
 * @since 2023-03-05
 */
@Api(description = "岗位管理")
@RestController
@RequestMapping("/servicejob/job")
@CrossOrigin
public class JobController {
    //把service注入
    @Autowired
    private JobService jobService;
    //查询job所有数据
    //rest风格
    @GetMapping("findAll")
    public R findAllJob(){
        List<Job> list = jobService.list(null);
        return R.ok().data("item",list);
    }
    //逻辑删除job
    @DeleteMapping("{id}")
    //@PathVariable String id得到路径中的id值
    public R removeJob(@PathVariable Integer id){
        boolean flag = jobService.removeById(id);
        if (flag){
            return R.ok();
        }else {
            return R.error();
        }
    }
    //3分页查询方法
    @GetMapping("pageJob/{current}/{limit}")
    public R pageListJob(@PathVariable long current,
                         @PathVariable long limit){
        //创建配置对象
        Page<Job> pageJob = new Page<>(current,limit);
        //调用方法实现分页
        //调用方法时，底层封装，把分页所有数据封装到pageJob对象里面
        jobService.page(pageJob,null);

        long total = pageJob.getTotal();//总记录数
        List<Job> records = pageJob.getRecords();//数据list集合
        return R.ok().data("total",total).data("rows",records);
    }
    //4条件查询带分页
    @PostMapping("pageJobCondition/{current}/{limit}")
    public R pageJobCondition(@PathVariable long current,@PathVariable long limit,
                              @RequestBody(required = false) JobQuery jobQuery){
        //创建page对象
        Page<Job> pageJob = new Page<>(current,limit);
        //构建条件
        QueryWrapper<Job> wrapper = new QueryWrapper<>();
        //多条件组合查询
        //动态sql
        //判断条件值是否为空，如果不空 拼接条件
        String job_name = jobQuery.getJob_name();
        String recruitment_tatus = jobQuery.getRecruitment_tatus();
        if(!StringUtils.isEmpty(job_name)){
            //构建条件
            wrapper.like("job_name",job_name);
        }
        if(!StringUtils.isEmpty(recruitment_tatus)){
            //构建条件
            wrapper.like("recruitment_tatus",recruitment_tatus);
        }
        //调用方法实现分页查询
        jobService.page(pageJob,wrapper);
        long total = pageJob.getTotal();//总记录数
        List<Job> records = pageJob.getRecords();//数据list集合
        return R.ok().data("total",total).data("rows",records);
    }
    //添加岗位接口方法
    @PostMapping("addJob")
    public R addJob(@RequestBody Job job){
        boolean save = jobService.save(job);
        if(save){
            return R.ok();
        }else {
           return R.error();
        }
    }
    //修改操作
    //1.根据id查询岗位
    @GetMapping("getJob/{id}")
    public R getJob(@PathVariable String id){
        Job job = jobService.getById(id);
        return R.ok().data("job",job);
    }
    //2.修改
    @PostMapping("updateJob")
    public R updateJob (@RequestBody Job job){
        boolean flag = jobService.updateById(job);
        if(flag){
            return R.ok();
        }else {
            return R.error();
        }
    }
}

